package cn.gwm.flink.streaming.config;

import cn.gwm.utils.GlobalConfigUtil;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Author: liangguang
 * @Date: 2023/02/18
 * @Description: 全局配置 - 数据精度
 */
@Getter
public enum DataPrecisionConfigEnum implements GlobalConfig {

    ODOMETER("odometer", "100", "数据精度 平台服务车辆，里程表低于？km，视为在售车辆，不进入平台"),
    PERIOD("period", "20", "数据精度 实时数仓最大学习周期：？周"),
    DISTANCE("distance", "20", "数据精度 位置精度，poi，经纬度误差？米"),

    ;

    /**
     * 显示
     */
    private String name;
    /**
     * 状态
     */
    private String value;
    /**
     * 描述
     */
    private String desc;

    DataPrecisionConfigEnum(String name, String value, String desc) {
        this.name = name;
        this.value = value;
        this.desc = desc;
    }

    @Override
    public void setValue(String value) {
        this.value = value;
    }

    @Override
    public String getValue() {
        // 访问更新
        DataPrecisionConfigUtil.controlUpdateConfig();
        return value;
    }

    @Slf4j
    static class DataPrecisionConfigUtil {
        private static final String TOPIC = "dataPrecision";
        private static final Integer MINUTE = 6;

        public static AtomicBoolean REQUEST_FLAG = new AtomicBoolean(true);

        public static AtomicInteger FIRST = new AtomicInteger(0);

        public static void controlUpdateConfig() {
            if (FIRST.get() == 0) {
                GlobalConfigUtil.requestConfig(TOPIC, DataPrecisionConfigEnum.values());
                FIRST.incrementAndGet();
            } else {
                // 每小时更新一次，6分
                if (DateUtil.date().getField(DateField.MINUTE) == MINUTE) {
                    if (REQUEST_FLAG.get()) {
                        GlobalConfigUtil.requestConfig(TOPIC, DataPrecisionConfigEnum.values());
                        REQUEST_FLAG.set(false);
                    }
                } else {
                    REQUEST_FLAG.set(true);
                }
            }

        }


    }
}
